package com.momo.auth.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.momo.auth.mapper.UserMapper;
import com.momo.auth.pojo.User;
import com.momo.auth.pojo.UserBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class JdbcUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserBo userBo = userMapper.selectOne(Wrappers.lambdaQuery(UserBo.class).eq(UserBo::getUsername, username));
        if (null == userBo){
            log.info("用户{}不存在", username);
            throw new UsernameNotFoundException(username);
        }
        User user = new User(userBo.getUsername(), userBo.getPassword(), userBo.getAuthorities(),userBo.getSex(),userBo.getPhone(),userBo.getId());
        log.info("登录成功！用户：{}", user.getUsername());
        return user;
    }
}
